package com.woniuxy.store.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.store.app.util.StoreIdUtil;
import com.woniuxy.store.client.StorePersonnelClient;
import com.woniuxy.store.client.param.AddPersonnelParam;
import com.woniuxy.store.client.param.LoginParam;
import com.woniuxy.store.client.param.ModifyPersonnelParam;
import com.woniuxy.store.client.result.LoginDto;
import com.woniuxy.store.client.result.StorePersonnelDto;
import com.woniuxy.store.common.util.JwtUtil;
import com.woniuxy.store.infra.model.StoreInfo;
import com.woniuxy.store.infra.model.StorePersonnel;
import com.woniuxy.store.service.StoreInfoService;
import com.woniuxy.store.service.StorePersonnelService;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.Result;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/store/manage")
public class StorePersonnelClientImpl implements StorePersonnelClient {

    @Resource
    private StorePersonnelService storePersonnelService;

    @Autowired
    private RedissonClient redissonClient;

    @Resource
    private StoreInfoService storeInfoService;


    @Override
    public Result addPersonnel(@Valid AddPersonnelParam param) {
        StorePersonnel personnel = BeanUtil.toBean(param, StorePersonnel.class);
        personnel.setStoreId(StoreIdUtil.getFromToken());
        storePersonnelService.addPersonnel(personnel);
        return Result.success();
    }

    @Override
    public Result modifyPersonnel(@Valid ModifyPersonnelParam param) {
        StorePersonnel personnel = BeanUtil.toBean(param, StorePersonnel.class);
        personnel.setStoreId(StoreIdUtil.getFromToken());
        storePersonnelService.modifyPersonnel(personnel);
        return Result.success();
    }

    @Override
    public Result delPersonnel( Integer personnelId) {
        storePersonnelService.delPersonnel(StoreIdUtil.getFromToken(),personnelId);
        return Result.success();
    }

    @Override
    public Result<StorePersonnelDto> getPersonnel() {
        List<StorePersonnel> list =  storePersonnelService.getPersonnel(StoreIdUtil.getFromToken());
        List<StorePersonnelDto> dtos = BeanUtil.copyToList(list, StorePersonnelDto.class);
        return Result.success(dtos);
    }

    @Override
    public Result<LoginDto> login(@Valid LoginParam param) {
        StorePersonnel storePersonnel = storePersonnelService.login(param.getPersonnelPhone(),param.getPersonnelPassword());
        System.out.println(storePersonnel);
        LoginDto loginDto = BeanUtil.toBean(storePersonnel, LoginDto.class);
        StoreInfo storeInfo = storeInfoService.getById(loginDto.getStoreId());
        HashMap<String,Object> body = new HashMap<String,Object>();
        body.put("storeId",loginDto.getStoreId());
        body.put("roleId",storeInfo.getRoleId());
        String token = JwtUtil.createToken(body, 30);
        String refreshToken = JwtUtil.createToken(body,8*60);
        RBucket<Object> bucket = redissonClient.getBucket(token);
        bucket.set(refreshToken,8*60*60, TimeUnit.SECONDS);
        //将jwt返回
        loginDto.setAccessToken(token);
        return Result.success(loginDto);
    }

}
